<link href="css/style.css" rel="stylesheet" type="text/css" />
<?php 
if(!isset($_SESSION)) 
	{ 
		session_start(); 
	}
$user=$_SESSION['fullname'];
?>
<?php
include 'operations/connection.php';
$query1 = mysql_query("SELECT e.Position ,dl.DepartmentName
							FROM employee e, department_lang dl 
							WHERE dl.IDDepartment=e.IDDepartment AND dl.LangID='$lang' AND e.IDEmployee='$id_employee';");
while ($row = mysql_fetch_array($query1))
{
	$Position = $row[Position];
	$DepartmentName = $row[DepartmentName];
}

if($_REQUEST['action']){
	$action=$_REQUEST['action'];
	$id=$_REQUEST['idapprovedoc'];
	if($_REQUEST['action']=="editform" || $_REQUEST['action']=="my_declined" || $_REQUEST['action']=="my_sending"){
		$sql="select Tile,EmployeePerforment,Location,TotalTime,Timecreate from approvedoc where IDApproveDoc='".$id."'";
		$mangnoidung= mysql_fetch_assoc(mysql_query($sql));
		$ftimecreate = $mangnoidung[Timecreate];
		$femperform=$mangnoidung[EmployeePerforment];
		$flocation=$mangnoidung[Location];
		$ftotaltimef = $mangnoidung[TotalTime];
		$ftitle=$mangnoidung[Tile];
		$query2 = mysql_query("select adv.items,adv.content,adv.money,adv.notes from ap_advance adv where document_id = '$id'");
		if(count($query2)>0){
			$_SESSION['listpayment']=array();
			while ($row = mysql_fetch_array($query2)){
				$_SESSION['listpayment'][]=array($row[items],$row[content],$row[money],$row[notes]);
				$listpayment=$_SESSION['listpayment'];
			}
		}else{
			$_SESSION['listpayment']=array();
			for($i=0;$i<5;$i++){
				$_SESSION['listpayment'][]=array("","",0,"");
			}
			$listpayment=$_SESSION['listpayment'];
		}
		
	}else if($_REQUEST['action']=="newform"){
		$ftimecreate = date("Y-m-d",time());
		$femperform="";
		$flocation="";
		$ftotaltimef ="";
		$ftitle="";
		$_SESSION['listpayment']=array();
		for($i=0;$i<5;$i++){
			$_SESSION['listpayment'][]=array("","",0,"");
		}
		$listpayment=$_SESSION['listpayment'];
	}
}


?>
<script type="text/javascript">

function addRowProduct()
{

	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
										  if (xmlhttp.readyState==4 && xmlhttp.status==200){
										    	document.getElementById("payment").innerHTML=xmlhttp.responseText;
										  }
									  }
	xmlhttp.open("GET","ajax/addRowProduct.php?action=add",true);
	xmlhttp.send();
}
function chItemProduct(str,str1)
{

	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
										  if (xmlhttp.readyState==4 && xmlhttp.status==200){
										    	document.getElementById("total").innerHTML=xmlhttp.responseText;
										  }
									  }
	xmlhttp.open("GET","ajax/chTotalProduct.php?action=chItem&value="+str+"&id="+str1,true);
	xmlhttp.send();
}
function chContentProduct(str,str1)
{

	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
										  if (xmlhttp.readyState==4 && xmlhttp.status==200){
										    	document.getElementById("total").innerHTML=xmlhttp.responseText;
										  }
									  }
	xmlhttp.open("GET","ajax/chTotalProduct.php?action=chContent&value="+str+"&id="+str1,true);
	xmlhttp.send();
}
function chCurencyProduct(str,str1)
{

	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
										  if (xmlhttp.readyState==4 && xmlhttp.status==200){
										    	document.getElementById("total").innerHTML=xmlhttp.responseText;
										  }
									  }
	if(str=='')
		str=0;
	else
		str=parseInt(str,10);
	document.getElementById("namec"+str1).value=str;
	xmlhttp.open("GET","ajax/chTotalProduct.php?action=chCurency&value="+str+"&id="+str1,true);
	xmlhttp.send();
}
function chRemarksProduct(str,str1)
{

	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
										  if (xmlhttp.readyState==4 && xmlhttp.status==200){
										    	document.getElementById("total").innerHTML=xmlhttp.responseText;
										  }
									  }
	xmlhttp.open("GET","ajax/chTotalProduct.php?action=chRemarks&value="+str+"&id="+str1,true);
	xmlhttp.send();
}
</script>

<script LANGUAGE="JavaScript" type="text/javascript">
function ch(){
	if(document.f17.title.value==""){
		alert('<?=$l_titlemusthavedate?>');
		document.f17.title.focus();
		return false;
	}
}

function focusOnMyInputBox(e,ten,co){
	var x;
    if(window.event) // IE8 and earlier
    	{
    	x=e.keyCode;
    	}
    else if(e.which) // IE9/Firefox/Chrome/Opera/Safari
    	{
    	x=e.which;
    	}
	if(x == 13)  {
		document.getElementById(ten).focus();
		return false;
	}else{
		if( x == 8 || x == 46  || x == 37 || x == 39 || co == '0'){
		    return true;
	    }
	    else if ( x < 48 || x > 57 ) {
	        return false;
	    }
	    else return true;
	}
}

function enterTable(e,value,count,co){
    var key = window.event ? e.keyCode : e.which;
    if (key == 13 || key == 8 || key == 46  || key == 37 || key == 39 || co == '0') {
		if(key == 13){
			var i = 0;
			var j = 0;
			var listID = new Array();
			for(i = 1; i <= count;i++){
		        listID = listID.concat(["namea"+i,"nameb"+i,"namec"+i,"named"+i]);	
			}
			if(value == listID[listID.length-1]){	
				document.getElementById("comment").focus();
				return false;			
			}else{
				for (j = 0; j < listID.length; j++){	
					if (listID[j] == value) {	
						if (j == listID.length - 1) {
							return false;
						}
						document.getElementById(listID[j + 1]).focus();
						document.getElementById(listID[j + 1]).select();
						return false; 
					}
				}
			}
			
			return false;
		}else
			return true;	
    } else if ( key < 48 || key > 57 ) {
        return false;
    }
    else return true;
				
}
</script>

<div class="nav_top">
       		<div class="nav_tit"><?php echo $l_susqence_payment_request?></div>     
            <div class="rehome"><a href="mainPhone.php"><?php echo $l_menu?></a></div>       
       	</div>
<form method="post" 	action="newform-handle.php" onsubmit="return ch()" name="f17">
	<input type="hidden" name='type' value="<?=$type?>" />
	<input type="hidden" name='action' value="<?=$action?>" />
	<input type="hidden" name='id' value="<?=$id?>" />
	<div class="main_content" style="min-height: 895px">
		<ul class="top_but">
			<li>
			<?php if($action=="my_declined" || $action=="my_sending"){?>
				<a href="mydocument-list-mydocument-saved-content"><img src="images/back.png" alt="" /> </a>
			<?php }else{?>
				
			<?php if(isset($_REQUEST['page'])){?>
				<a href="<?=$_SERVER['HTTP_REFERER']?>&page=<?=$_REQUEST['page']?>"><img src="images/back.png" alt="" /> </a>
			<?php }else{?>
				<a href="<?=$_SERVER['HTTP_REFERER']?>"><img src="images/back.png" alt="" /> </a>
			<?php } 
			} ?>
			
			
				<p class="hidden" >
				<?=$l_back?>
				</p>
			</li>
			<li><input type="submit" name="Save" value=""
				style="background: url(images/save.png) no-repeat; width: 36px; height: 36px; border: none; position: absolute; top:0px; left:0px; right:0px; bottom:0px; margin: auto" />
				<p class="hidden">
				<?=$l_save?>
				</p></li>
			<li><input type="submit" name="Send" value=""
				style="background: url(images/send.png) no-repeat; width: 36px; height: 36px; border: none; position: absolute; top:0px; left:0px; right:0px; bottom:0px; margin: auto" />
				<p class="hidden" >
				<?=$l_send_document?>
				</p></li>
			<?php 
			
				if($_REQUEST['action']){
					if($_REQUEST['action']=="editform" || $_REQUEST['action']=="my_declined"){
						?>
						
						<li><input type="submit" name="Delete" value=""
						style="background: url(images/deny.png) no-repeat; width: 36px; height: 36px; border: none; position: absolute; top:0px; left:0px; right:0px; bottom:0px; margin: auto" />
						<p class="hidden">
						<?=$l_delete?>
						</p></li>
						
						<?php 
					}
				}
			
			?>
		</ul>
		<table class="tcontent" cellpadding="5" cellspacing="0">
			<tr>
				<td class="col_left"><?php echo $l_createdby?></td>
				<td class="col_left" width="15%"><?php echo $l_department?></td>
				<td><?php echo $DepartmentName?></td>
				<td width="12%" class="col_left"><?php echo $l_position?></td>
				<td width="12%"><?php echo $Position?></td>
				<td class="col_left"><?php echo $l_fullname?></td>
				<td><?php echo $user?></td>
			</tr>
			<tr>
				<td width="15%" rowspan="2" class="col_left"><?php echo $l_project_purpose?>
				</td>
				<td width="35%" colspan="2" rowspan="2"><textarea name="title" id="title"
						cols="5" rows="3" style="width: 96%; border: 1px solid #c4c0c9"><?php echo $ftitle?></textarea>
				</td>
				<td rowspan="2" class="col_left"><?php echo $l_bussiness_date?></td>
				<td class="col_left"><?php echo $l_location?></td>
				<td colspan="2"><input type="text" id="location" name="location" onkeypress ="return focusOnMyInputBox(event,'employeeper','0');"  value="<?php echo $flocation?>"
					style="height: 24px; width: 100%; border: 1px solid #c4c0c9;line-height: 24px;" /></td>
			</tr>
			<tr>
				<td class="col_left"><?php echo $l_partner?></td>
				<td colspan="2"><input type="text"  id="employeeper" name="employeeper" onkeypress ="return focusOnMyInputBox(event,'paymentadvance','0');"  value="<?php echo $femperform; ?>"
					style="height: 24px; width: 100%; border: 1px solid #c4c0c9;line-height: 24px;" /></td>
			</tr>
			<tr>
				<td class="col_left"><?php echo $l_created_date?></td>
				<td colspan="6"><?php echo $ftimecreate; ?></td>
			</tr>
			<tr>
				<td class="col_left"><?php echo $l_payment_advance?></td>
				<td colspan="6"><input type="text" onkeypress ="return focusOnMyInputBox(event,'namea1','1');" id="paymentadvance" name="paymentadvance" value="<?php echo $ftotaltimef;?>"
					style="height: 24px; width: 100%; border: 1px solid #c4c0c9;line-height: 24px;" /></td>
			</tr>
			<tr>
				<td colspan="7" style="font-size: 18px"><?php echo $l_title_table?>
				</td>
			</tr>
		</table>
		<div id="payment">
		<table class="tcontent" cellpadding="5" cellspacing="0" id="tblView">
			<tr class="trtit">
				<td width="5%"></td>
				<td width="15%"><?php echo $l_items?>(*)</td>
				<td width="50%"><?php echo $l_content?></td>
				<td width="15%"><?php echo $l_currency?></td>
				<td width="15%"><?php echo $l_remarks?></td>
			</tr>
			<?php 
				$i=1;
				$total=0;
				foreach ($listpayment as $payment){?>
				<tr id="my_row">
				<td><?=$i?></td>
				<td><input type="text" onkeypress="return enterTable(event,this.id,<?php echo count($listpayment);?>,'0')" id="<?echo "namea".$i?>" name="item[]" onchange="chItemProduct(this.value,<?=$i?>)" onkeyup="chItemProduct(this.value,<?=$i?>)" style="height: 24px; width: 96%; border: 1px solid #c4c0c9;line-height: 24px;" value="<?=$payment[0]?>" /></td>
				<td><input type="text" onkeypress="return enterTable(event,this.id,<?php echo count($listpayment);?>,'0')" id="<?echo "nameb".$i?>" name="contentitem[]" onchange="chContentProduct(this.value,<?=$i?>)"  onkeyup="chContentProduct(this.value,<?=$i?>)"	style="height: 24px; width: 96%; border: 1px solid #c4c0c9;line-height: 24px;" value="<?=$payment[1]?>" /></td>
				<td><input type="text" onkeypress="return enterTable(event,this.id,<?php echo count($listpayment);?>,'1')" id="<?echo "namec".$i?>" name="currency[]" onchange="chCurencyProduct(this.value,<?=$i?>)"  onkeyup="chCurencyProduct(this.value,<?=$i?>)" style="height: 24px; width: 96%; border: 1px solid #c4c0c9;line-height: 24px;" value="<?=$payment[2]?>" autocomplete="off"  /></td>
				<td><input type="text" onkeypress="return enterTable(event,this.id,<?php echo count($listpayment);?>,'0')" id="<?echo "named".$i?>" name="remark[]" onchange="chRemarksProduct(this.value,<?=$i?>)"  onkeyup="chRemarksProduct(this.value,<?=$i?>)" style="height: 24px; width: 96%; border: 1px solid #c4c0c9;line-height: 24px;" value="<?=$payment[3]?>" /></td>
				</tr>
				<?php 
				$i=$i+1;
				$total +=$payment[2];
				}
				?>
		</table>
		</div>
		<table class="tcontent" cellpadding="5" cellspacing="0" style="border-top: none !important;">
			<tr>
				<td style="text-align: left" colspan="4">
				<input type="button" onclick="addRowProduct()"	value="Add more" id="btnAddMore" class="btnupbg btnupload" /><!--  >span style="color: red">(*)<?php echo $l_required;?></span--></td>
				<td style="border:none !important;"></td>
			</tr>
			<tr>
				<td  width="20%" colspan="2"><?php echo $l_total?></td>
				<td width="50%"></td>
				
				<td width="15%" ><div id="total"><?php echo number_format($total)?></div></td>
				<td width="15%"style="border:none !important;"></td>
			</tr>
		</table>
	</div>
</form>